<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);

use think\App;
use think\facade\Db;

require_once "WxPay.Api.php";
require_once 'WxPay.Notify.php';


class WxOrderNotify extends WxPayNotify
{
    protected $app;
    public function __construct ( App $app )
    {
        $this->app = $app;
    }
    //查询订单
    public function Queryorder($transaction_id){
        @file_put_contents('./weixinQuery.txt','in_query',FILE_APPEND);
        $input = new WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = WxPayApi::orderQuery($input);
        if(array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
            $arr = json_decode($result['attach'] , true);
            if(is_array($arr) && $arr['type']=='PayGoods'){
                $resunt = new \app\shop\controller\IndexPayResunt($this->app);
                $res    = $resunt->payResunt($arr,$transaction_id);
                return $res;
            }else if(is_array($arr) && $arr['type']=='PayQr'){
                $index = new \app\payqr\controller\IndexPayResunt($this->app);
                $rest = $index->qr_notify($arr,$transaction_id);
                return $rest;
            }else if(is_array($arr) && $arr['type']=='subscribe'){
                $user = new \app\bodybuild\controller\User($this->app);
                $rest = $user->subscribe_notify($arr,$transaction_id);
                return $rest;
            }else if(is_array($arr) && $arr['type']=='redbag'){
                //红包活动模型
                $active_model = new \app\redbag\model\IndexActive();
                //修改状态
                $rest = $active_model->payResult($arr['out_trade_no'],$transaction_id);

                return $rest;
            }else if(is_array($arr) && $arr['type']=='reduction'){
                //满减扫码支付
                $pay_model = new \app\reduction\model\PayRecord();
                //修改状态
                $rest = $pay_model->payResult($arr['out_trade_no'],$transaction_id);

                return $rest;
            }else if(is_array($arr) && $arr['type']=='storedPay'){
                //储值支付
                $pay_model = new \app\member\model\StoredOrder();
                //修改状态
                $rest = $pay_model->orderResult($arr['out_trade_no'],$transaction_id);

                return $rest;
            }else if(is_array($arr) && $arr['type']=='restaurant'){
                //储值支付
                $pay_model = new \app\restaurant\model\Order();
                //修改状态
                $rest = $pay_model->orderResult($arr['out_trade_no'],$transaction_id);

                return $rest;
            }else if(is_array($arr) && $arr['type']=='overlord'){
                //储值支付
                $pay_model = new \app\overlord\model\Order();
                //修改状态
                $rest = $pay_model->orderResult($arr['out_trade_no'],$transaction_id);

                return $rest;
            }else if(is_array($arr) && $arr['type']=='PayClass'){
                //购买课程
                $pay_model = new \app\payclass\model\Order();
                //修改状态
                $rest = $pay_model->orderResult($arr['out_trade_no'],$transaction_id);

                return $rest;
            }else if(is_array($arr) && $arr['type']=='helppay'){
                $resunt = new \app\shop\controller\IndexPayResunt($this->app);
                $res    = $resunt->helpPayResunt($arr,$transaction_id);
                return $res;
            }else if(is_array($arr) && $arr['type']=='banquetPlace'){
                //宴会厅场地预约
                $resunt = new \app\banquet\model\Order();

                $res    = $resunt->orderResult($arr['out_trade_no'],$transaction_id);

                return $res;
            }
        }
        return false;
    }

    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {
        $notfiyOutput = array();

        if(!array_key_exists("transaction_id", $data)){
            file_put_contents('./weixinQuery.txt','输入参数不正确',FILE_APPEND);
            $msg = "输入参数不正确";
            return false;
        }
        file_put_contents('./weixinQuery.txt','abc',FILE_APPEND);
        //查询订单，判断订单真实性
        if(!$this->Queryorder($data["transaction_id"],$data[''])){
            $msg = "订单查询失败";
            return false;
        }
        return true;
    }
}


